#include "weather_lable.h"

#include <QList>
#include <QPainter>
#include <QPaintEvent>

Weather_lable::Weather_lable(QWidget *parent) : QLabel(parent)
{
    // 初始化温度数组
    memset(high_temp, 0, sizeof(high_temp));
    memset(low_temp, 0, sizeof(low_temp));
}

void Weather_lable::paintEvent(QPaintEvent *event)
{
    // 先调用父类的paintEvent，确保背景等正常绘制
    QLabel::paintEvent(event);

    if (high_temp[0] == 0 && low_temp[0] == 0) {
        return; // 没有数据时不绘制
    }

    // 计算X坐标（7天数据，平均分布）
    int xStep = width() / 8; // 留出左右边距
    for (int i = 0; i < 7; ++i) {
        high_temp_point[i].setX(xStep * (i + 1));
        low_temp_point[i].setX(xStep * (i + 1));
    }

    // 计算温度极值（合并高低温计算整体范围）
    int tempMax = -100;
    int tempMin = 100;
    for (int i = 0; i < 7; ++i) {
        tempMax = qMax(tempMax, qMax(high_temp[i], low_temp[i]));
        tempMin = qMin(tempMin, qMin(high_temp[i], low_temp[i]));
    }

    // 处理温度范围为0的特殊情况
    if (tempMax == tempMin) {
        tempMax += 1;
        tempMin -= 1;
    }

    // 计算Y坐标（预留上下边距50）
    int yRange = height() - 100; // 可用高度
    double scale = static_cast<double>(yRange) / (tempMax - tempMin);
    
    for (int i = 0; i < 7; ++i) {
        // 高温点Y坐标（从上到下绘制）
        int highY = 50 + static_cast<int>((tempMax - high_temp[i]) * scale);
        high_temp_point[i].setY(highY);
        
        // 低温点Y坐标
        int lowY = 50 + static_cast<int>((tempMax - low_temp[i]) * scale);
        low_temp_point[i].setY(lowY);
    }

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true); // 抗锯齿

    // 绘制高温线
    QPen penHigh(QColor(255, 100, 100), 2); // 红色系
    painter.setPen(penHigh);
    QPainterPath highPath;
    highPath.moveTo(high_temp_point[0]);
    for (int i = 1; i < 7; ++i) {
        highPath.lineTo(high_temp_point[i]);
    }
    painter.drawPath(highPath);

    // 绘制高温点
    painter.setBrush(QColor(255, 100, 100));
    for (int i = 0; i < 7; ++i) {
        painter.drawEllipse(high_temp_point[i], 5, 5);
        painter.drawText(high_temp_point[i].x() - 10, high_temp_point[i].y() - 15, 
                         QString::number(high_temp[i]) + "°");
    }

    // 绘制低温线
    QPen penLow(QColor(100, 100, 255), 2); // 蓝色系
    painter.setPen(penLow);
    QPainterPath lowPath;
    lowPath.moveTo(low_temp_point[0]);
    for (int i = 1; i < 7; ++i) {
        lowPath.lineTo(low_temp_point[i]);
    }
    painter.drawPath(lowPath);

    // 绘制低温点
    painter.setBrush(QColor(100, 100, 255));
    for (int i = 0; i < 7; ++i) {
        painter.drawEllipse(low_temp_point[i], 5, 5);
        painter.drawText(low_temp_point[i].x() - 10, low_temp_point[i].y() + 25, 
                         QString::number(low_temp[i]) + "°");
    }
}

void Weather_lable::get_weather_temp(QList<Forecast> temp)
{
    if (temp.length() < 7) {
        qDebug() << "天气数据不足7天";
        return;
    }

    for (int i = 0; i < 7; ++i) {
        // 提取数字并转换为整数
        high_temp[i] = temp[i].high.remove(QRegExp("[^0-9]")).toInt();
        low_temp[i] = temp[i].low.remove(QRegExp("[^0-9]")).toInt();
    }
    update(); // 触发重绘
}

void Weather_lable::mouseDoubleClickEvent(QMouseEvent *event)
{
    update();
}
